package com.t18;

import java.io.*;
import java.util.Enumeration;
import java.util.zip.*;

import static net.mindview.util.Print.print;

/**
 * Created by xuyiwei on 2018/9/4.
 */
public class ZipCompress {
    public static void main(String[] args)
            throws IOException {
        FileOutputStream f = new FileOutputStream("/Users/xuyiwei/Documents/javaBase/jdk/src/com/t18/test.zip");
        //CheckedInputStream和CheckedOutputStream用来生成带验证的解压缩  用户计算的MD5跟你给的MD5是否一样  已确定文件完整性
        CheckedOutputStream csum =
                new CheckedOutputStream(f, new Adler32());
        ZipOutputStream zos = new ZipOutputStream(csum);
        BufferedOutputStream out =
                new BufferedOutputStream(zos);
        zos.setComment("A test of Java Zipping");
        // No corresponding getComment(), though.
        String[] aargs = {"/Users/xuyiwei/Documents/javaBase/jdk/src/com/t18/a.txt",
                "/Users/xuyiwei/Documents/javaBase/jdk/src/com/t18/b.txt",
                "/Users/xuyiwei/Documents/javaBase/jdk/src/com/t18/c.txt",
                "/Users/xuyiwei/Documents/javaBase/jdk/src/com/t18/d.txt",
                "/Users/xuyiwei/Documents/javaBase/jdk/src/com/t18/e.txt"};
        String[] aags = {"/a.txt",
                "/b.txt",
                "/c.txt",
                "/d.txt",
                "/e.txt"};
        for(int i=0;i<aargs.length;i++) {
            print("Writing file " + aargs[i]);
            BufferedReader in =
                    new BufferedReader(new FileReader(aargs[i]));
            //对于每一个要加入压缩文档的文件，都必须调用putNextEntry()，并将其传递给一个ZipEntry()，并将其传递给ZipEntry对象
            //如果不把流放入压缩包输出流的话  就会导致写入失败
            zos.putNextEntry(new ZipEntry(aags[i]));
            int c;
            while((c = in.read()) != -1)
                out.write(c);
            in.close();
            out.flush();
        }
        out.close();
        // 只有在流关闭后才可以校验值
        print("Checksum: " + csum.getChecksum().getValue());
        // Now extract the files:
        print("Reading file");
        FileInputStream fi = new FileInputStream("/Users/xuyiwei/Documents/javaBase/jdk/src/com/t18/test.zip");
        CheckedInputStream csumi =
                new CheckedInputStream(fi, new Adler32());
        ZipInputStream in2 = new ZipInputStream(csumi);
        BufferedInputStream bis = new BufferedInputStream(in2);
        ZipEntry ze;
        while((ze = in2.getNextEntry()) != null) {
            print("Reading file " + ze);
            int x;
            while((x = bis.read()) != -1)
                System.out.write(x);
        }
        if(args.length == 1)
            print("Checksum: " + csumi.getChecksum().getValue());
        bis.close();
        // 得到压缩包的所有文件
        ZipFile zf = new ZipFile("/Users/xuyiwei/Documents/javaBase/jdk/src/com/t18/test.zip");
        Enumeration e = zf.entries();
        while(e.hasMoreElements()) {
            ZipEntry ze2 = (ZipEntry)e.nextElement();
            print("File: " + ze2);
            // ... and extract the data as before
        }
    /* if(args.length == 1) */
    }
}
